﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Windows.Forms;

namespace UIT_CV_Management
{
    public class TotNghiep
    {
        public String maTotNghiep;
        private String tenDoAnTotNghiep;
        private float ketQuaDoAn;
        public String maMonThiToiNghiep;

        // object
        public List<MonThiTotNghiep> listMonThiTotNghiep
        {
            get
            {
                QueryResult result_MonThi = 
                    myDatabase.Query("SELECT * FROM MonThiTotNghiep WHERE MaMonThiTotNghiep = '" + 
                    maMonThiToiNghiep + "'");
                if (result_MonThi.ReturnCode == 200)
                {
                    listMonThiTotNghiep = new List<MonThiTotNghiep>();
                        
                    for (int i = 0; i < result_MonThi.Table.Rows.Count; i++)
                    {
                        DataRow row = result_MonThi.Table.Rows[i];
                        MonThiTotNghiep monThi = new MonThiTotNghiep(row["ID"].ToString());
                        listMonThiTotNghiep.Add(monThi);
                    }
                }
                return listMonThiTotNghiep;
            }
            set
            {
                listMonThiTotNghiep = value;
            }
        }
        
        //Database
        Database myDatabase = Database.shareDatabase;

        public TotNghiep(String _tenDoAn, float _ketQuaDoAn, List<MonThiTotNghiep> _listMonThiTotNghiep)
        {
            maTotNghiep = this.getMaTotNghiepFromDatabase();

            tenDoAnTotNghiep = _tenDoAn;
            ketQuaDoAn = _ketQuaDoAn;
            
            //object
            listMonThiTotNghiep = _listMonThiTotNghiep;
            maMonThiToiNghiep =  _listMonThiTotNghiep[0].maMonThiTotNghiep;
        }
        public TotNghiep(String _MaTotNghiep)
        {
            QueryResult result =  myDatabase.Query("SELECT * FROM TotNghiep WHERE MaTotNghiep = '" + _MaTotNghiep + "'");
            if (result.ReturnCode == 200)
            {
                DataTable table = result.Table;
                maTotNghiep = table.Rows[0]["MaTotNghiep"].ToString();
                tenDoAnTotNghiep = table.Rows[0]["TenDoAnTotNghiep"].ToString();
                ketQuaDoAn = float.Parse(table.Rows[0]["KetQuaDoAn"].ToString());
                maMonThiToiNghiep = table.Rows[0]["MaMonThiTotNghiep"].ToString();
            }
        }
        private String getMaTotNghiepFromDatabase()
        {
            String x;
            x = Database.getID("TotNghiep", "MaTotNghiep");
            return x;
        }
        public void save()
        {
            String sqlTotNghiep = "INSERT INTO TotNghiep VALUES('" + maTotNghiep + "','" + tenDoAnTotNghiep + "'," + ketQuaDoAn + ",'" + maMonThiToiNghiep + "')";
            QueryResult result = myDatabase.Query(sqlTotNghiep);
            if (result.ReturnCode == 409)
                MessageBox.Show("Lỗi khi lưu dữ liệu vào database", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            else // save successfully
            {
                foreach (MonThiTotNghiep monThi in listMonThiTotNghiep)
                {
                    monThi.save();
                }
            }
        }
        public void update()
        {
            bool isDupplicated = Database.isDupplicate("TotNghiep", "MaTotNghiep", maTotNghiep);

            // update
            if (isDupplicated)
            {
                String sqlUpdate = "UPDATE TotNgiep SET TenDoAnTotNghiep = '" + tenDoAnTotNghiep + "', KetQuaDoAn = " + ketQuaDoAn
                    + " WHERE MaTotNgiep = '" + maTotNghiep + "'";
                QueryResult result = myDatabase.Query(sqlUpdate);

                if (result.ReturnCode == 409)
                    MessageBox.Show("Lỗi khi update liệu Thông Tin Phụ vào database", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                else
                {
                    
                }
            }
        }
        public void delete()
        {
            String sqlDelete = "DELETE FROM TotNghiep WHERE MaTotNghiep = '" + maTotNghiep + "'";
            QueryResult result = myDatabase.Query(sqlDelete);

            if (result.ReturnCode == 409)
                MessageBox.Show("Lỗi xoá dữ liệu trong database.", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            else
            {
                foreach (MonThiTotNghiep monThi in listMonThiTotNghiep)
                {
                    monThi.delete();
                }
            }
        }

    }
}
